<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>上传PDF文件</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome 图标 -->
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background-color: #f8f9fa;
        }
        .upload-container {
            max-width: 500px;
            margin: 100px auto;
            background-color: #fff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .upload-container h2 {
            text-align: center;
            margin-bottom: 20px;
        }
        .form-group {
            margin-bottom: 1.5rem;
        }
        .btn-upload {
            width: 100%;
        }
        .progress {
            height: 30px;
            margin-top: 15px;
        }
        .progress-bar {
            font-size: 1rem;
            line-height: 30px;
        }
        .alert {
            display: none;
            margin-top: 15px;
        }
    </style>
</head>
<body>

<div class="upload-container">
    <h2>上传PDF文件</h2>
    <form id="uploadForm" enctype="multipart/form-data">
        <div class="form-group">
            <label for="pdfFile">选择PDF文件</label>
            <input type="file" class="form-control" id="pdfFile" name="file" accept=".pdf" required>
        </div>
        <button type="submit" class="btn btn-primary btn-upload">上传</button>
    </form>
    <div class="progress mt-3" id="progressBar" style="display: none;">
        <div class="progress-bar" role="progressbar" style="width: 0%;" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100">0%</div>
    </div>
    <div class="alert" id="message"></div>
</div>

<!-- Bootstrap JS 和依赖（Popper.js） -->
<script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.11.6/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.min.js"></script>
<script>
    document.getElementById('uploadForm').addEventListener('submit', function(e) {
        e.preventDefault(); // 防止表单默认提交

        const form = e.target;
        const formData = new FormData(form);
        const fileInput = document.getElementById('pdfFile');
        const progressBar = document.getElementById('progressBar');
        const message = document.getElementById('message');

        if (fileInput.files.length === 0) {
            alert('请选择一个PDF文件');
            return;
        }

        // 显示进度条
        progressBar.style.display = 'block';

        fetch('http://localhost:9000/api/pdf/excel', {
            method: 'POST',
            body: formData
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error(`服务器响应失败: ${response.statusText}`);
                }
                return response.json();
            })
            .then(data => {
                message.className = 'alert alert-success';
                message.textContent = '上传成功！';
                message.style.display = 'block';
                progressBar.querySelector('.progress-bar').style.width = '100%';
                progressBar.querySelector('.progress-bar').textContent = '100%';
                // 可选：重置表单
                form.reset();
                // 3秒后隐藏消息
                setTimeout(() => {
                    message.style.display = 'none';
                }, 3000);
            })
            .catch(error => {
                console.error('上传出错:', error);
                message.className = 'alert alert-danger';
                message.textContent = '上传失败，请稍后再试。';
                message.style.display = 'block';
            });
    });
</script>

</body>
</html>